<html>
<head><meta charset="utf-8"><title>noalias / Stacked Borrows vs self-referential genreators · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html">noalias / Stacked Borrows vs self-referential genreators</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="174490670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174490670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174490670">(Aug 29 2019 at 20:42)</a>:</h4>
<p><span class="user-mention" data-user-id="127859">@Taylor Cramer</span> after you left, <span class="user-mention" data-user-id="116009">@nikomatsakis</span> and <span class="user-mention" data-user-id="126931">@centril</span> and me talked some more, and the conclusion is that we can likely find <em>some</em> model that will make generators work. Given how desirable that use-case is, that seems like a constraint worth putting on the model, even if it complicates things. And given that we don't emit <code>noalias</code> for mutable reference ATM per default, it's also not super-urgent tog et this fixed because right now we are not misoptimizing anything (we generate UB-free LLVM IR, for all we know).<br>
It is still a soundness bug though to generate UB-ful LLVM IR with <code>-Zmutable-noalias</code>. <span class="user-mention" data-user-id="116009">@nikomatsakis</span> proposed fix is to search for generators inside mutable references (similar to how we search for <code>UnsafeCell</code> inside shard references), and inhibit the attribute when we find one.</p>



<a name="174491029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174491029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174491029">(Aug 29 2019 at 20:47)</a>:</h4>
<p>Is it enough to just turn of <code>noalias</code> on the references to generators while still having <code>noalias</code> on the references within the interior?</p>



<a name="174491055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174491055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174491055">(Aug 29 2019 at 20:48)</a>:</h4>
<p>I'd imagine that depends on whether you ever use the reference to the generator to derive a reference to the interior</p>



<a name="174491133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174491133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174491133">(Aug 29 2019 at 20:48)</a>:</h4>
<p>but I don't have a good sense for how the actual formal rules around <code>noalias</code> interact in this case</p>



<a name="174491163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174491163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174491163">(Aug 29 2019 at 20:49)</a>:</h4>
<p>(storing derived pointers)</p>



<a name="174492372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174492372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174492372">(Aug 29 2019 at 21:03)</a>:</h4>
<p>storing a pointer has no effect on that pointer. it keeps its identity.</p>



<a name="174492409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174492409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174492409">(Aug 29 2019 at 21:03)</a>:</h4>
<blockquote>
<p>I'd imagine that depends on whether you ever use the reference to the generator to derive a reference to the interior</p>
</blockquote>
<p>you are not allowed to derive a reference to the offending field (the one that has an outstanding loan)</p>



<a name="174498939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174498939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174498939">(Aug 29 2019 at 22:42)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Yeah, so we can't ever debug print values that have existing mutable references to them</p>



<a name="174498942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174498942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174498942">(Aug 29 2019 at 22:43)</a>:</h4>
<p>which makes sense</p>



<a name="174522911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174522911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174522911">(Aug 30 2019 at 07:48)</a>:</h4>
<blockquote>
<p>which makes sense</p>
</blockquote>
<p>glad that we agree on that :)</p>



<a name="174531234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174531234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174531234">(Aug 30 2019 at 10:03)</a>:</h4>
<blockquote>
<p>similar to how we search for UnsafeCell inside shard references</p>
</blockquote>
<p>By the way, does this currently pessimize type-erased contexts where the type is hidden under <code>dyn Trait</code> or something like that?</p>



<a name="174565846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174565846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174565846">(Aug 30 2019 at 17:33)</a>:</h4>
<p>Hello, i was reading <a href="https://github.com/rust-lang/rust/issues/63818#issuecomment-526579977" target="_blank" title="https://github.com/rust-lang/rust/issues/63818#issuecomment-526579977">https://github.com/rust-lang/rust/issues/63818#issuecomment-526579977</a> . I think the direction is great, but the rules feels a little ad-hoc, and i feel if user defined something and its behavior is similar to generators, it won't be protected by the rule, if we only focus on the generator use case.</p>



<a name="174566439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174566439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174566439">(Aug 30 2019 at 17:40)</a>:</h4>
<p>I'm trying to make up my own theory, which might be similar but different in details.<br>
I'm thinking about use a special named lifetime called <code>'self</code> to mark the self-referential lifetime.</p>
<p>So for the original problem:</p>
<div class="codehilite"><pre><span></span>pub async fn test() -&gt; i32 {
    let a: Cell&lt;i32&gt; = Cell::new(0);
    let mut b: &amp;Cell&lt;i32&gt; = &amp;a;
    SomeFuture.await;
    b.set(100);
    a.get()
}
</pre></div>


<p>I think it can desugar into</p>
<div class="codehilite"><pre><span></span>struct Test {
    state: ?,
    a: Cell&lt;i32&gt;,
    b: &amp;&#39;self Cell&lt;i32&gt;,
}
</pre></div>


<p>And when <code>'self</code> is used in a struct <code>S</code>, it means it can be self-referential. And <code>&amp;mut S</code> is no longer <code>noalias</code>.</p>



<a name="174566824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174566824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174566824">(Aug 30 2019 at 17:44)</a>:</h4>
<p>Maybe a more powerful version of borrowck in the future can build rules around this special lifetime, and reason about it.</p>



<a name="174578051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174578051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174578051">(Aug 30 2019 at 20:02)</a>:</h4>
<p><span class="user-mention" data-user-id="116458">@Charles Lew</span> lifetimes are erased, and cannot have different runtime behavior</p>



<a name="174609526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174609526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174609526">(Aug 31 2019 at 09:04)</a>:</h4>
<blockquote>
<blockquote>
<p>similar to how we search for UnsafeCell inside shard references</p>
</blockquote>
<p>By the way, does this currently pessimize type-erased contexts where the type is hidden under <code>dyn Trait</code> or something like that?</p>
</blockquote>
<p>currently actually not, if you have an <code>&amp;dyn Trait</code> Miri will figure out the "real" type and treat the reference accordingly. whether that is waht we want, I do not know.</p>



<a name="174620129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/noalias%20/%20Stacked%20Borrows%20vs%20self-referential%20genreators/near/174620129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/noalias.20.2F.20Stacked.20Borrows.20vs.20self-referential.20genreators.html#174620129">(Aug 31 2019 at 14:31)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="116458">Charles Lew</span> lifetimes are erased, and cannot have different runtime behavior</p>
</blockquote>
<p>I thought named lifetimes can be a little special? But yes this system might be over-complex. I was just thinking about how to make the mechanism able to map to surface syntax.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>